from flask import Flask
from flask_cors import CORS
from routes.user_routes import user_bp, initialize_user_bp
from routes.course_routes import course_bp
from routes.order_routes import order_bp
# from routes.download import download_bp, read_config

from tools.db import Database
import os
from routes.timeOut import start_timeout_scheduler  # Corrected import
# 获取当前工作目录
current_dir = os.path.dirname(os.path.abspath(__file__))
config_file = os.path.join(current_dir, 'config', 'config.ini')
config_file_download = os.path.join(current_dir, 'config', 'download.ini')
config_user = os.path.join(current_dir, 'config', 'user.ini')
# 初始化数据库连接池
try:
    Database.initialize(config_file)
except Exception as e:
    print(f"Failed to initialize database: {e}")
    exit(1)
# username, password = read_config(config_file_download)

app = Flask(__name__)
app.config.from_object('payment_config.Config')
initialize_user_bp(config_user)
CORS(app, resources=r'/*')  # Allow cross-origin requests for all routes

# Register blueprints
app.register_blueprint(user_bp)
app.register_blueprint(course_bp)
app.register_blueprint(order_bp)
# app.register_blueprint(download_bp)
# Start scheduler
scheduler = start_timeout_scheduler()


if __name__ == '__main__':
    app.run(debug=True)